package fr.pentalog.ctc.controller.page;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import fr.pentalog.ctc.util.WebAppConstants;

import ro.peoplecentric.ctc.common.entity.Item;
import ro.peoplecentric.ctc.common.exception.ApplicationException;
import ro.peoplecentric.ctc.common.service.ItemService;

/**
 * @author Danut Chindris
 * @since September 18, 2013
 */

@Controller
public class DetailsController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	public static final String DETAILS_VIEW_NAME = "pages/details";

	@Resource
	private ItemService itemService;

	@RequestMapping(value = { "/details" }, method = RequestMethod.GET)
	public String showItemsPage(Model model, @RequestParam Long id) {
		Item item = null;
		if (id != null) {
			try {
				item = itemService.getById(id);
			}
			catch (ApplicationException e) {
				logger.error(e.getMessage(), e);
			}
		}
		if (item != null) {
			model.addAttribute(WebAppConstants.ARTICLE_ATTRIBUTE, item);
		}
		return DETAILS_VIEW_NAME;
	}
}
